﻿using System;
using UnityEngine;

// Token: 0x02000151 RID: 337
[AddComponentMenu("RVP/C#/Effects/Tire Screech Audio", 1)]
[DisallowMultipleComponent]
[RequireComponent(typeof(AudioSource))]
public class TireScreech : MonoBehaviour
{
	// Token: 0x0600060F RID: 1551 RVA: 0x00031B74 File Offset: 0x0002FD74
	private void Start()
	{
		this.snd = base.GetComponent<AudioSource>();
		this.vp = (VehicleParent)F.GetTopmostParentComponent<VehicleParent>(base.transform);
		this.wheels = new Wheel[this.vp.wheels.Length];
		for (int i = 0; i < this.vp.wheels.Length; i++)
		{
			this.wheels[i] = this.vp.wheels[i];
			if (this.vp.wheels[i].GetComponent<TireMarkCreate>())
			{
				float num = this.vp.wheels[i].GetComponent<TireMarkCreate>().slipThreshold;
				this.slipThreshold = ((i != 0) ? ((this.slipThreshold + num) * 0.5f) : num);
			}
		}
	}

	// Token: 0x06000610 RID: 1552 RVA: 0x00031C44 File Offset: 0x0002FE44
	private void Update()
	{
		float num = 0f;
		bool flag = true;
		bool flag2 = true;
		float num2 = 0f;
		for (int i = 0; i < this.vp.wheels.Length; i++)
		{
			if (this.wheels[i].connected)
			{
				if (Mathf.Abs(F.MaxAbs(new float[]
				{
					this.wheels[i].sidewaysSlip,
					this.wheels[i].forwardSlip,
					num2
				})) - this.slipThreshold > 0f)
				{
					if (this.wheels[i].popped)
					{
						flag2 = false;
					}
					else
					{
						flag = false;
					}
				}
				if (this.wheels[i].grounded)
				{
					this.surfaceType = GroundSurfaceMaster.surfaceTypesStatic[this.wheels[i].contactPoint.surfaceType];
					if (this.surfaceType.alwaysScrape)
					{
						num2 = this.slipThreshold + Mathf.Min(0.5f, Mathf.Abs(this.wheels[i].rawRPM * 0.001f));
					}
				}
				num = Mathf.Max(num, Mathf.Pow(Mathf.Clamp01(Mathf.Abs(F.MaxAbs(new float[]
				{
					this.wheels[i].sidewaysSlip,
					this.wheels[i].forwardSlip,
					num2
				})) - this.slipThreshold), 2f));
			}
		}
		if (this.surfaceType != null)
		{
			this.snd.clip = ((!flag) ? ((!flag2) ? this.surfaceType.tireRimSnd : this.surfaceType.tireSnd) : this.surfaceType.rimSnd);
		}
		if (num > 0f)
		{
			if (!this.snd.isPlaying)
			{
				this.snd.Play();
				this.snd.volume = 0f;
			}
			else
			{
				this.snd.volume = Mathf.Lerp(this.snd.volume, num * ((float)this.vp.groundedWheels * 1f / ((float)this.wheels.Length * 1f)), 2f * Time.deltaTime);
				this.snd.pitch = Mathf.Lerp(this.snd.pitch, 0.5f + num * 0.9f, 2f * Time.deltaTime);
			}
		}
		else if (this.snd.isPlaying)
		{
			this.snd.volume = 0f;
			this.snd.Stop();
		}
	}

	// Token: 0x04000962 RID: 2402
	private AudioSource snd;

	// Token: 0x04000963 RID: 2403
	private VehicleParent vp;

	// Token: 0x04000964 RID: 2404
	private Wheel[] wheels;

	// Token: 0x04000965 RID: 2405
	private float slipThreshold;

	// Token: 0x04000966 RID: 2406
	private GroundSurface surfaceType;
}
